<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>书籍每日订阅</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-warning-outline"
            size="small" 
            type="text"
            @click="lookChapterInfo(scope.row)"
            >章节详情</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { handleExcelCat } from "@/api/exportCommon";
import { appStatisticsTodayBookSubs as baseUrl } from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        // selection: true,
        border: true,
        menu: true,
        index: true,
        indexLabel: "序号",
        column: [
          {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            hide:true
          },
          {
            label: "日期",
            prop: "create_time",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
          },
          {
            label: "客户端",
            prop: "source",
            type: "select",
            dicData: [
              {
                label: "安卓",
                value: 0,
              },
              {
                label: "IOS",
                value: 1,
              },
              {
                label: "pc",
                value: 2,
              },
              {
                label: "m端",
                value: 3,
              },
              {
                label: "快应用",
                value: 4,
              },
              {
                label: "微信小程序",
                value: 5,
              },
              {
                label: "QQ小程序",
                value: 6,
              }
            ]
          },
          {
            label: "书籍id",
            type: "input",
            prop: "book_id",
            search: true,
          },
          {
            label: "书籍名称",
            prop: "book_title",
            type: "input",
            search: true,
          },
          {
            label: "CPId",
            prop: "cp_id",
            type: "input",
            search: true,
            hide:true
          },
          {
            label: "CP方",
            prop: "cp_name",
            type: "input",
            search: true,
          },
          {
            label: "当日订阅章节数",
            prop: "today_subscription_chapter_num",
            type: "input",
          },
          {
            label: "当日订阅书籍人数",
            prop: "today_subscription_people_num",
            type: "input",
          },
          {
            label: "当日人均订阅章节数",
            prop: "today_avg_subscription_chapter_num",
            type: "input",
          }
        ],
      },
    };
  },
  methods: {
   //数据导出
     async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm}; 
      params.route = "数据/书籍/书籍每日订阅";

      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000; 

      if(params.statistics_date_search.length>0){
        export_condition += " &时间区间："+params.statistics_date_search[0].toLocaleDateString()+"~"+params.statistics_date_search[1].toLocaleDateString()
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }
      if(params.book_id){
        export_condition += " &书籍id："+params.book_id;
      }
      if(params.book_title){
        export_condition += " &书籍名称："+params.book_title;
      }
      if(params.cp_id){
        export_condition += " &cpid："+params.cp_id;
      }
      params.export_condition = export_condition == "" ? "所有" : export_condition;

     //调用导出接口
      let response = await common(
        baseUrl.exportBookTodaySubscribeData,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseUrl.appStatisticsTodayBookSubs}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if(params.statistics_date_search){
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }
      
      this.params = params;
      this.page.currentPage = 1;
      delete params.daterange;
      this.getList();
      if (done) {
        done();
      }
    },
    // 跳转
    lookChapterInfo(scope) {
      this.$router.push({
        path: "/chaptersubs/chaptersubscription",
        query: {
          book_id: scope.book_id,
          source: scope.source,
          book_title: `《${scope.book_title}》`,
          preRouter: this.$route.path,
        },
      });
    },
  },
};
</script>